<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:权限菜单管理
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Access as cModel;
use support\Request;
use support\Util\InitTree;
use app\BackendController;

class AccessController extends BackendController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    function list() {
        $ac_pid = request()->get("ac_pid/d", 0);
        return view('access/list', ['ac_pid' => $ac_pid]);
    }

    public function listData(Request $req)
    {
        $w = [];

        $req->get('ac_name') && $w[] = ['ac_name', 'like', '%' . $req->get('ac_name') . '%'];
        $req->get('ac_id') && $w[]   = ['ac_id', '=', $req->get('ac_id')];
        $ls                            = cModel::where($w)->field("*,ac_id as id,ac_pid  as  parentid,ac_name as name")->order("ac_order", "asc")->select()->each(function ($item, $key) {
            $item->ac_menu_flag_txt = $item->ac_menu_flag == 1 ? '是' : '否';
            return $item;
        })->toArray();
        $ls   = array_column($ls, null, "id");
        $tree = new InitTree;
        $tree->set_tree($ls);
        $ls = $tree->get_childs(0);
        $ls = array_values($ls);
        return json(['code' => count($ls) > 0 ? 0 : 201, 'msg' => '无数据', 'data' => $ls, 'count' => count($ls)]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        $ls = cmodel::field("*,ac_id as id,ac_pid  as  parentid,ac_name as name")->order("ac_order", "asc")->select()->toArray();

        $cat_options = '';
        if ($ls) {
            $ls   = array_column($ls, null, "ac_id");
            $tree = new InitTree;
            $tree->set_tree($ls);
            $cat_options = $tree->get_tree(0, 0);
        }

        return view('access/create', compact('cat_options'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post     = $request->post();
        $validate = new \app\admin\validate\RoleAccess;
        $result   = $validate->check($post);
        if (!$result) {
            $data = ['msg' => $validate->getError(), 'status' => -1];
        } else {
            $cmodel = new cModel();
            $cmodel->save($post);
            $data = ['msg' => '保存成功', 'status' => 1];
        }
        return json($data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id   = $request->get("id");
        $data = cmodel::find($id);
        return view("access/edit", ['u_id' => $id, 'data' => $data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $id       = $request->get("id");
        $post     = $request->post();
        $validate = new \app\admin\validate\RoleAccess;
        $result   = $validate->check($post);
        if (!$result) {
            $data = ['msg' => $validate->getError(), 'status' => -1];
        } else {
            $cmodel = new cModel();
            $cmodel->where('ac_id', $id)->save($post);

            $data = ['msg' => '保存成功', 'status' => 1];
        }
        return json($data);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request->get("id");

        $f = cmodel::where('ac_pid', 'in', $id)->count();
        if ($f > 0) {
            $data = ['msg' => '存在子分类，请先删除子分类', 'status' => 0];
        } else {
            cmodel::where('ac_id', 'in', $id)->delete();
            $data = ['msg' => '删除成功', 'status' => 1];
        }
        return json($data);

    }

    public function changeValue()
    {
        $ac_id     = (int) $this->request->post("ac_id");
        $field     = $this->request->post("field");
        $value     = $this->request->post("value");
        $f         = cmodel::where('ac_id', $ac_id)->find();
        $f->$field = $value;
        $f->save();
        $data = ['msg' => '保存成功', 'status' => 1];
        return json($data);
    }
}
